/**
 * 
 */
package com.objectdraw.server.commands;

import com.objectdraw.server.CommunicationServer;
import com.objectdraw.server.ObjectDrawServer;
import com.objectdraw.server.data.RequestData;
import com.objectdraw.server.data.ResponseData;

/**
 * Processes tool change requests from the client.
 * @author Harrison
 */
public class ToolChangeCommand extends Command {
	static final private String myCommand = "tool";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.objectdraw.server.commands.Command#processRequest(com.objectdraw.server.RequestData,
	 *      com.objectdraw.server.ResponseData)
	 */
	/**
	 * Takes a message with the command "tool" and sets the current tool to the one in the mesage.  Also broadcasts
	 * the message to all listening clients.
	 */
	@Override
	public void processRequest(RequestData request, ResponseData response) {
		if (myCommand.equalsIgnoreCase(request.getCommand())) {
			System.out.println(myCommand + " Command");
			/**
			 * Tool: tool <tool> <clientname>
			 */
			if (request.getParams() != null) {
				ObjectDrawServer server = ObjectDrawServer.getInstance();
				String currentDrawer = server.getCurrentDrawer();
				String[] params = request.getParams().split(" ");
				if (params.length > 1) {
					if (currentDrawer.equalsIgnoreCase(params[1])) {
						System.out.println("Server: Tool set to " + params[0]);
						
						//TODO: maybe only do this code if the current tool
						//is not set to the requested tool
						server.setCurrentTool(params[0]);
						
						//Broadcast the new thing
						response.addResponse("tool " + params[0]);
						CommunicationServer.broadcast(response, server.getViewingClientSockets());
					}
				}
			}
		}

		// Pass the request on to the next person in line
		if (this.successor != null) {
			this.successor.processRequest(request, response);
		}
	}
}
